package com.ts.api.module.api.convert.core.http.client;

import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.net.http.HttpRequestHeader;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.common.constant.api.ApiBizTypeEnum;

/**
 *  HTTP消息类
 *
 *  @author Pan
 */
public class ApiHttpMessage {

    /** ----------------------------------------请求---------------------------------------- */
    /** 发送地址 */
    private String url;
    /** API业务类型 */
    private ApiBizTypeEnum apiBizTypeEnum;
    /** 请求体 */
    private Object requestBody;
    /** 原始请求参数(json格式) */
    private String sourceRequestBody;
    /** 关闭UrlEncode */
    private Boolean disableUrlEncode = false;
    /** 提交方式 */
    private HttpContentType httpContentType;
    /** 请求头 */
    private HttpRequestHeader httpRequestHeader;
    /** 超时时长毫秒 */
    private Integer timeout = 60000;

    /** 请求id */
    private Long reqId;
    /** 手机号MD5 */
    private String phoneMd5;

    /** ----------------------------------------响应---------------------------------------- */
    /** 是否超时 */
    private Boolean hasReqTimeout = false;
    /** 响应体 */
    private String responseBody;
    /** 是否业务成功 */
    private Boolean hasSuc = false;
    /** 服务响应码 */
    private Integer respCode;
    /** 响应错误消息 */
    private String errorMsg;
    /** 堆栈信息 */
    private String stackMsg;

    public ApiHttpMessage() {
        super();
    }

    public ApiHttpMessage(String url, ApiBizTypeEnum apiBizTypeEnum) {
        this.url = url;
        this.apiBizTypeEnum = apiBizTypeEnum;
    }

    public String getSourceRequestBody() {
        return sourceRequestBody;
    }

    public void setSourceRequestBody(String sourceRequestBody) {
        this.sourceRequestBody = sourceRequestBody;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Object getRequestBody() {
        return requestBody;
    }

    public void setRequestBody(Object requestBody) {
        this.requestBody = requestBody;
    }

    public String getResponseBody() {
        return responseBody;
    }

    public void setResponseBody(String responseBody) {
        this.responseBody = responseBody;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public HttpContentType getHttpContentType() {
        return httpContentType;
    }

    public void setHttpContentType(HttpContentType httpContentType) {
        this.httpContentType = httpContentType;
    }

    public HttpRequestHeader getHttpRequestHeader() {
        return httpRequestHeader;
    }

    public void setHttpRequestHeader(HttpRequestHeader httpRequestHeader) {
        this.httpRequestHeader = httpRequestHeader;
    }

    public Integer getRespCode() {
        return respCode;
    }

    public void setRespCode(Integer respCode) {
        this.respCode = respCode;
    }

    public String getStackMsg() {
        return stackMsg;
    }

    public void setStackMsg(String stackMsg) {
        this.stackMsg = stackMsg;
    }

    public ApiBizTypeEnum getApiBizTypeEnum() {
        return apiBizTypeEnum;
    }

    public void setApiBizTypeEnum(ApiBizTypeEnum apiBizTypeEnum) {
        this.apiBizTypeEnum = apiBizTypeEnum;
    }

    public boolean getDisableUrlEncode() {
        return disableUrlEncode;
    }

    public void setDisableUrlEncode(boolean disableUrlEncode) {
        this.disableUrlEncode = disableUrlEncode;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public void setHasSuc(Boolean hasSuc) {
        this.hasSuc = hasSuc;
    }

    public Boolean hasSuc() {
        return hasSuc;
    }

    public void setHasReqTimeout(Boolean hasReqTimeout) {
        this.hasReqTimeout = hasReqTimeout;
    }

    public Boolean hasReqTimeout() {
        return hasReqTimeout;
    }

    public Long getReqId() {
        return reqId;
    }

    public void setReqId(Long reqId) {
        this.reqId = reqId;
    }

    public String getPhoneMd5() {
        return phoneMd5;
    }

    public void setPhoneMd5(String phoneMd5) {
        this.phoneMd5 = phoneMd5;
    }

    /**
     *  设置异常信息
     *
     *  @param e    异常信息
     */
    public void setExceptionInfo(Exception e) {
        //  机构流程中出现异常
        setErrorMsg(e.toString());
        StringBuilder sb = StringUtils.createBuilder(e).append("\n");
        StackTraceElement[] stackTrace = e.getStackTrace();

        for (int i = 0; i < stackTrace.length; i++) {
            StackTraceElement stackTraceElement = stackTrace[i];

            if (ValidParam.isNull(getErrorMsg()) && i == 0) {
                String string = stackTraceElement.toString();
                setErrorMsg(string);
            }

            String stackTraceElementString = stackTraceElement.toString();
            if (stackTraceElementString.contains("com.ts")) {
                sb.append("\t" + stackTraceElementString).append("\n");
            }
        }
        setStackMsg(sb.toString());
    }

    /**
     *  构建消息
     *
     *  @param  url             地址
     *  @param  apiBizTypeEnum  业务类型
     *  @return ApiHttpMessage
     */
    public static ApiHttpMessage create(String url, ApiBizTypeEnum apiBizTypeEnum) {
        return new ApiHttpMessage(url, apiBizTypeEnum);
    }
}
